var api     = require('../../utils/api.js');
var WxParse = require('../../wxParse/wxParse.js');
var app     = getApp();
Page({
    data: { switch:true,total:0,piece1:0,piece2:0,piece3:0,post:[] },
    onLoad(params) {
        this.params = params;
    },

    onReady() {
        this.loadData();
    },
    loadData() {
        var params = this.params;
        if (params.id) {
            wx.showNavigationBarLoading();
            api.put({
                url: 'logi/Userlogis/read',
                data: {'id':params.id},
                success: data => {
                    wx.hideNavigationBarLoading();
                    if (data.code) {
                        var switch_tab = true;
                        if (data.data.user_mail_type==2) {
                            switch_tab = false;
                        };
                        this.setData({
                            post: data.data,
                            switch: switch_tab,
                            total: data.data.price,
                            piece1: data.data.more.total,
                            piece2: data.data.more.piece,
                            piece3: data.data.more.price
                        });
                        wx.setNavigationBarTitle({
                            title: data.data.post_title
                        });
                    }
                },
                fail: err => {
                    wx.hideNavigationBarLoading();
                }
            });
        }
    },
    formSubmit(e) {
        var logi_id = this.params.id;
        //console.log(e);
        api.post({
            url: 'logi/Userlogis/toPay',
            data: {
                id: logi_id,
                form_id: e.detail.formId,
                switch: this.data.switch,
            },
            success: data => {
                if (data.code) {
                    wx.requestPayment( {
                        'timeStamp': data.data.timeStamp,
                        'nonceStr': data.data.nonceStr,
                        'package': data.data.package,
                        'signType': data.data.signType,
                        'paySign': data.data.paySign,
                        'success':function(res){
                            console.log(res);
                            api.post({
                                url: 'logi/Userlogis/paySucceed',
                                data: {id: logi_id},
                                success: res => {
                                    if (res.code == 1) {
                                        wx.showToast({
                                            title: '支付成功',
                                            icon: 'success',
                                            duration: 800
                                        })
                                        setTimeout(function(){ wx.switchTab({ url: '/pages/index/index'}); }, 800);
                                    }
                                }
                            });
                        },
                        'fail':function(res){
                            console.log(res);
                            wx.showToast({
                                title: '支付失败',
                                icon: 'loading',
                                duration: 1000
                            });
                        }
                    })

                } else {
                    wx.showToast({
                        title: '操作失败',
                        icon: 'loading',
                        duration: 1000
                    });
                }
            }
        });
    },
    formClose(e) {
        var del_id = this.params.id;
        var form_id = e.detail.formId;
        wx.showModal({
            content: '是否确认删除？',
            showCancel: true,
            success: function (res) {
                if (res.confirm) {
                    api.post({
                        url: 'logi/Userlogis/close',
                        data: {id: del_id,form_id:form_id},
                        success: data => {
                            if (data.code == 1) {
                                wx.showToast({
                                    title: data.msg,
                                    icon: 'success',
                                    duration: 800
                                })
                                setTimeout(function(){ wx.switchTab({ url: '/pages/index/index'}); }, 800);
                            }
                        }
                    });
                }
            }
        })
    },
    switchChange: function(e) {
        this.setData({ switch: e.detail.value });
        if (e.detail.value) {
            this.setData({ total: this.data.piece1 })
        } else {
            this.setData({ total: this.data.piece2 })
        }
        var logi_id = this.params.id;
        //console.log(e);
        api.post({
            url: 'logi/Userlogis/switchTab',
            data: { 
                id: logi_id,
                switch:e.detail.value,
                total:this.data.post.more.total,
                piece:this.data.post.more.piece,
                price:this.data.post.more.price
            },
            success: data => {}
        });
        
    },
    codeCopy(e){
        wx.setClipboardData({
            data: e.currentTarget.dataset.msg,  //+this.data.result.fin_code
            success: function(res) {
                wx.getClipboardData({
                    success: function(res) {
                        wx.showToast({
                            title: '复制成功',
                            icon: 'success',
                            duration: 800
                        })
                    }
                })
            }
        })
        // var detail = e.detail;
        // api.post({
        //     url: 'user/address/update',
        //     data: {form_id: detail.formId},
        //     success: res => {
                
        //     }
        // });
    },
    goExpress(e) {
        wx.navigateTo({ url: '/pages/logi/express?id='+this.params.id+'&status='+e.currentTarget.dataset.mail_status});
    },
    onShareTap() {
        wx.showShareMenu({
            withShareTicket: true
        });
    },
    onShareAppMessage() {
        return {
            title: '樱花驿站',
            path: '/pages/index/index'
        }
    }

});
